package com.game.pacman.client;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.net.URL;
import java.util.LinkedList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class Player{
	
	private Image image;//표시될 이미지
	private Image image1,image2;
		
	private boolean imageState; 
	private int x, y;//위치 정보
	private int dx, dy;//이동량
	private int direction;


	public Player() {
		//this.image = image;
		//y = Constants.WINDOW_HEIGHT/2;
		//x = Constants.WINDOW_WIDTH/2;
//		x = 325;
//		y=362;

 		//1인용
		int posX=0;
		int posY=0;

		//2인용
//		int posX=25;
//		int posY=0;
// 
		x=posX*Constants.OBJECT_WIDTH+Constants.BUTTON_WIDTH;
		y=posY*Constants.OBJECT_HEIGHT-Constants.OBJECT_HEIGHT;
//		y=0;
		
		try{
		URL url = getClass().getResource("/image/pacman2.gif");
		Image image1 = Toolkit.getDefaultToolkit().getImage(url);
		this.image = image1.getScaledInstance(Constants.OBJECT_WIDTH, Constants.OBJECT_HEIGHT,Image.SCALE_SMOOTH);
		}catch(Exception e){
			
		}
	}
	
	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}
	

	public void draw(Graphics g) {
		g.drawImage(image, x, y, null);
	}
	
	public void update() {
		x += dx;
		y += dy;
		if (x >= Constants.BUTTON_WIDTH)	
			x-=Constants.OBJECT_WIDTH;
		if (y >= 0) y-=Constants.OBJECT_HEIGHT;
		if (x <= Constants.WINDOW_WIDTH - Constants.OBJECT_WIDTH*2 -Constants.BUTTON_WIDTH )
			x+=Constants.OBJECT_WIDTH;
		if (y <= Constants.MAP_HEIGHT -Constants.OBJECT_HEIGHT*2)
			y+=Constants.OBJECT_HEIGHT;
			
	}
	
	public void keyPressedHandler(KeyEvent e) {
		int code = e.getKeyCode();
		switch (code) {
		case KeyEvent.VK_LEFT :
		case KeyEvent.VK_A :
			direction = Constants.LEFT;
			URL url = getClass().getResource("/image/pacman_lf.gif");
			Image image1 = Toolkit.getDefaultToolkit().getImage(url);
			this.image = image1.getScaledInstance(Constants.OBJECT_WIDTH, Constants.OBJECT_HEIGHT,Image.SCALE_SMOOTH);
			//dx = -1*(Constants.OBJECT_WIDTH/1);
			
			break;
		case KeyEvent.VK_RIGHT :
		case KeyEvent.VK_D : 			
			direction = Constants.RIGHT;
			URL url2 = getClass().getResource("/image/pacman2.gif");
			Image image2 = Toolkit.getDefaultToolkit().getImage(url2);
			this.image = image2.getScaledInstance(Constants.OBJECT_WIDTH, Constants.OBJECT_HEIGHT,Image.SCALE_SMOOTH);
			//dx = 1*(Constants.OBJECT_WIDTH/1);
			break;
		case KeyEvent.VK_UP :
		case KeyEvent.VK_W :
			direction = Constants.UP;
			URL url3 = getClass().getResource("/image/pacman_up.gif");
			Image image3 = Toolkit.getDefaultToolkit().getImage(url3);
			this.image = image3.getScaledInstance(Constants.OBJECT_WIDTH, Constants.OBJECT_HEIGHT,Image.SCALE_SMOOTH);
			//dy = -1*(Constants.OBJECT_HEIGHT/1);
			break;
		case KeyEvent.VK_DOWN :
		case KeyEvent.VK_S :
			direction = Constants.DOWN;
			URL url4 = getClass().getResource("/image/pacman_dn.gif");
			Image image4 = Toolkit.getDefaultToolkit().getImage(url4);
			this.image = image4.getScaledInstance(Constants.OBJECT_WIDTH, Constants.OBJECT_HEIGHT,Image.SCALE_SMOOTH);
			//dy = 1*(Constants.OBJECT_HEIGHT/1);
			break;
		}
	}
	public void keyReleasedHandler(KeyEvent e) {
		int code = e.getKeyCode();
		switch (code) {
		case KeyEvent.VK_LEFT :
		case KeyEvent.VK_A :
		case KeyEvent.VK_RIGHT :
		case KeyEvent.VK_D : 
			dx = 0;
			break;
		case KeyEvent.VK_UP :
		case KeyEvent.VK_W :
		case KeyEvent.VK_DOWN :
		case KeyEvent.VK_S :
			dy = 0;
			break;
		}
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getWidth() {
		return image.getWidth(null);
	}
	public int getHeight() {
		return image.getHeight(null);
	}

	public int getDx() {
		return dx;
	}

	public void setDx(int dx) {
		this.dx = dx;
	}

	public int getDy() {
		return dy;
	}

	public void setDy(int dy) {
		this.dy = dy;
	}

//	public Image getImage() {
//		return image;
//	}

//	public void setImage(boolean imageState) {
//		if(imageState) 
//		{
//			this.image = image1.getScaledInstance(Constants.OBJECT_WIDTH, Constants.OBJECT_HEIGHT,Image.SCALE_SMOOTH);
//			this.imageState=false;
//		}else{
//			this.image = image2.getScaledInstance(Constants.OBJECT_WIDTH, Constants.OBJECT_HEIGHT,Image.SCALE_SMOOTH);
//			this.imageState=true;
//		}
//	}
//
//	public boolean isImageState() {
//		return imageState;
//	}
//
//	public void setImageState(boolean imageState) {
//		this.imageState = imageState;
//	}	
}